package com.qiantu.phone.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.SettingBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.dialog.BottomTimePeriodDialog;
import com.lxj.xpopup.XPopup;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class MessageSettingActivity extends AppActivity {

    private SwitchButton btnSmartMessageSwitch;
    private SwitchButton btnAlarmMessageSwitch;
    private SwitchButton btnNoticeMessageSwitch;
    private TextView tvUndisturbedPeriod;
    private View btnUndisturbed;
    private BottomTimePeriodDialog mBottomTimePeriodDialog;
    private SettingBean.SettingDTO.MessageDTO data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message_setting;
    }

    @Override
    protected void initView() {
        btnSmartMessageSwitch = findViewById(R.id.btn_smart_message_switch);
        btnAlarmMessageSwitch = findViewById(R.id.btn_alarm_message_switch);
        btnNoticeMessageSwitch = findViewById(R.id.btn_notice_message_switch);
        tvUndisturbedPeriod = findViewById(R.id.tv_undisturbed_period);
        btnUndisturbed = findViewById(R.id.btn_undisturbed);
        setOnClickListener(R.id.btn_smart_message_switch, R.id.btn_alarm_message_switch, R.id.btn_notice_message_switch, R.id.btn_undisturbed);
    }

    @Override
    protected void initData() {
        data = new SettingBean.SettingDTO.MessageDTO();
        LLHttpManager.getMessageSetting(this, new HttpCallback<HttpData<SettingBean.SettingDTO.MessageDTO>>(this){
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.common_loading));
            }

            @Override
            public void onSucceed(HttpData<SettingBean.SettingDTO.MessageDTO> result) {
                data = result.getData();
                updateView();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void updateView(){
        if(data != null){
            btnSmartMessageSwitch.setChecked(data.isReceiveIntelligent());
            btnAlarmMessageSwitch.setChecked(data.isReceiveWarning());
            btnNoticeMessageSwitch.setChecked(data.isReceiveNotice());
            if(!btnSmartMessageSwitch.isChecked()
                && !btnAlarmMessageSwitch.isChecked()
                    && !btnNoticeMessageSwitch.isChecked()){
                btnUndisturbed.setVisibility(View.GONE);
            }else{
                btnUndisturbed.setVisibility(View.VISIBLE);
            }
            if(data.isOpenMute()){
                setLockTime(data.getStartTime(), data.getEndTime());
            }else{
                tvUndisturbedPeriod.setText(R.string.not_enabled);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_smart_message_switch:
                data.setReceiveIntelligent(btnSmartMessageSwitch.isChecked());
                btnSmartMessageSwitch.setChecked(!btnSmartMessageSwitch.isChecked());
                setting();
                break;
            case R.id.btn_alarm_message_switch:
                data.setReceiveWarning(btnAlarmMessageSwitch.isChecked());
                btnAlarmMessageSwitch.setChecked(!btnAlarmMessageSwitch.isChecked());
                setting();
                break;
            case R.id.btn_notice_message_switch:
                data.setReceiveNotice(btnNoticeMessageSwitch.isChecked());
                btnNoticeMessageSwitch.setChecked(!btnNoticeMessageSwitch.isChecked());
                setting();
                break;
            case R.id.btn_undisturbed:
                showBottomTimePeriodDialog();
                break;
        }
    }

    private void setting(){
        Map<String, Object> params = new HashMap<>();
        params.put("receiveIntelligent", data.isReceiveIntelligent());
        params.put("receiveWarning", data.isReceiveWarning());
        params.put("receiveNotice", data.isReceiveNotice());
        params.put("openMute", data.isOpenMute());
        if(TextUtils.isEmpty(data.getStartTime())){
            params.put("startHour", 0);
            params.put("startMinute", 0);
        }else{
            String[] startTime = data.getStartTime().split(":");
            params.put("startHour", startTime[0]);
            params.put("startMinute", startTime[1]);
        }
        if(TextUtils.isEmpty(data.getEndTime())){
            params.put("endHour", 0);
            params.put("endMinute", 0);
        }else{
            String[] endTime = data.getEndTime().split(":");
            params.put("endHour", endTime[0]);
            params.put("endMinute", endTime[1]);
        }
        LLHttpManager.setMessageSetting(this, params, new HttpCallback<HttpData<Void>>(this){
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.setting));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                toast(R.string.set_success);
                updateView();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void showBottomTimePeriodDialog(){
        if (mBottomTimePeriodDialog == null) {
            mBottomTimePeriodDialog = new BottomTimePeriodDialog(this);
            mBottomTimePeriodDialog.setDialogClickListener(new BottomTimePeriodDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(boolean isOpen, String startHour, String startMinute, String endHour, String endMinute) {
                    if(isOpen){
                        data.setStartTime(startHour + ":" + startMinute);
                        data.setEndTime(endHour + ":" + endMinute);
                    }else{
                        data.setStartTime(null);
                        data.setEndTime(null);
                    }
                    data.setOpenMute(isOpen);
                    mBottomTimePeriodDialog.dismiss();
                    setting();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomTimePeriodDialog);
        }
        String period = tvUndisturbedPeriod.getText().toString();
        String start = null;
        String end = null;
        boolean isOpen;
        if(period.contains("~")){
            isOpen = true;
            start = period.split("~")[0];
            end = period.split("~")[1].replace("次日", "");
        }else{
            isOpen = false;
            start = "00:00";
            end = "00:00";
        }
        mBottomTimePeriodDialog.setSelectedTime(isOpen, start, end);
        mBottomTimePeriodDialog.show();
    }

    private void setLockTime(String start, String end){
        if(TextUtils.isEmpty(start) || TextUtils.isEmpty(end)){
            return;
        }
        String[] startHM = start.split(":");
        String[] endHM = end.split(":");
        int startH = Integer.valueOf(startHM[0]);
        int startM = Integer.valueOf(startHM[1]);
        int endH = Integer.valueOf(endHM[0]);
        int endM = Integer.valueOf(endHM[1]);
        String nextDay = "";
        //结束时间小于开始时间
        if(startH >= endH || (startH == endH && startM >= endM)){
            nextDay = "次日";
        }
        tvUndisturbedPeriod.setText(start +  "~" + nextDay + end);
    }
}